1
|
|
|
var shellwords = require('shellwords'); |
2
|
|
|
var cp = require('child_process'); |
3
|
|
|
var semver = require('semver'); |
4
|
|
|
var path = require('path'); |
5
|
|
|
var url = require('url'); |
6
|
|
|
var os = require('os'); |
7
|
|
|
var fs = require('fs'); |
8
|
|
|
|
9
|
|
|
function clone(obj) { |
10
|
|
|
return JSON.parse(JSON.stringify(obj)); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
module.exports.clone = clone; |
14
|
|
|
|
15
|
|
|
var escapeQuotes = function(str) { |
16
|
|
|
if (typeof str === 'string') { |
17
|
|
|
return str.replace(/(["$`\\])/g, '\\$1'); |
18
|
|
|
} else { |
|
|
|
|
19
|
|
|
return str; |
20
|
|
|
} |
21
|
|
|
}; |
22
|
|
|
|
23
|
|
|
var inArray = function(arr, val) { |
24
|
|
|
return arr.indexOf(val) !== -1; |
25
|
|
|
}; |
26
|
|
|
|
27
|
|
|
var notifySendFlags = { |
28
|
|
|
u: 'urgency', |
29
|
|
|
urgency: 'urgency', |
30
|
|
|
t: 'expire-time', |
31
|
|
|
time: 'expire-time', |
32
|
|
|
e: 'expire-time', |
33
|
|
|
expire: 'expire-time', |
34
|
|
|
'expire-time': 'expire-time', |
35
|
|
|
i: 'icon', |
36
|
|
|
icon: 'icon', |
37
|
|
|
c: 'category', |
38
|
|
|
category: 'category', |
39
|
|
|
subtitle: 'category', |
40
|
|
|
h: 'hint', |
41
|
|
|
hint: 'hint' |
42
|
|
|
}; |
43
|
|
|
|
44
|
|
|
module.exports.command = function(notifier, options, cb) { |
45
|
|
|
notifier = shellwords.escape(notifier); |
46
|
|
|
if (process.env.DEBUG && process.env.DEBUG.indexOf('notifier') !== -1) { |
47
|
|
|
console.info('node-notifier debug info (command):'); |
48
|
|
|
console.info('[notifier path]', notifier); |
49
|
|
|
console.info('[notifier options]', options.join(' ')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return cp.exec(notifier + ' ' + options.join(' '), function( |
53
|
|
|
error, |
54
|
|
|
stdout, |
55
|
|
|
stderr |
56
|
|
|
) { |
57
|
|
|
if (error) return cb(error); |
|
|
|
|
58
|
|
|
cb(stderr, stdout); |
|
|
|
|
59
|
|
|
}); |
60
|
|
|
}; |
61
|
|
|
|
62
|
|
|
module.exports.fileCommand = function(notifier, options, cb) { |
63
|
|
|
if (process.env.DEBUG && process.env.DEBUG.indexOf('notifier') !== -1) { |
64
|
|
|
console.info('node-notifier debug info (fileCommand):'); |
65
|
|
|
console.info('[notifier path]', notifier); |
66
|
|
|
console.info('[notifier options]', options.join(' ')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return cp.execFile(notifier, options, function(error, stdout, stderr) { |
70
|
|
|
if (error) return cb(error, stdout); |
|
|
|
|
71
|
|
|
cb(stderr, stdout); |
|
|
|
|
72
|
|
|
}); |
73
|
|
|
}; |
74
|
|
|
|
75
|
|
|
module.exports.fileCommandJson = function(notifier, options, cb) { |
76
|
|
|
if (process.env.DEBUG && process.env.DEBUG.indexOf('notifier') !== -1) { |
77
|
|
|
console.info('node-notifier debug info (fileCommandJson):'); |
78
|
|
|
console.info('[notifier path]', notifier); |
79
|
|
|
console.info('[notifier options]', options.join(' ')); |
80
|
|
|
} |
81
|
|
|
return cp.execFile(notifier, options, function(error, stdout, stderr) { |
82
|
|
|
if (error) return cb(error, stdout); |
|
|
|
|
83
|
|
|
if (!stdout) return cb(error, {}); |
|
|
|
|
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
var data = JSON.parse(stdout); |
87
|
|
|
cb(stderr, data); |
|
|
|
|
88
|
|
|
} catch (e) { |
89
|
|
|
cb(e, stdout); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
}); |
92
|
|
|
}; |
93
|
|
|
|
94
|
|
|
module.exports.immediateFileCommand = function(notifier, options, cb) { |
95
|
|
|
if (process.env.DEBUG && process.env.DEBUG.indexOf('notifier') !== -1) { |
96
|
|
|
console.info('node-notifier debug info (notifier):'); |
97
|
|
|
console.info('[notifier path]', notifier); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
notifierExists(notifier, function(_, exists) { |
101
|
|
|
if (!exists) { |
102
|
|
|
return cb(new Error('Notifier (' + notifier + ') not found on system.')); |
103
|
|
|
} |
104
|
|
|
cp.execFile(notifier, options); |
105
|
|
|
cb(); |
|
|
|
|
106
|
|
|
}); |
107
|
|
|
}; |
108
|
|
|
|
109
|
|
|
function notifierExists(notifier, cb) { |
110
|
|
|
return fs.stat(notifier, function(err, stat) { |
111
|
|
|
if (!err) return cb(err, stat.isFile()); |
|
|
|
|
112
|
|
|
|
113
|
|
|
// Check if Windows alias |
114
|
|
|
if (path.extname(notifier)) { |
115
|
|
|
// Has extentioon, no need to check more |
116
|
|
|
return cb(err, false); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Check if there is an exe file in the directory |
120
|
|
|
return fs.stat(notifier + '.exe', function(err, stat) { |
121
|
|
|
if (err) return cb(err, false); |
|
|
|
|
122
|
|
|
cb(err, stat.isFile()); |
|
|
|
|
123
|
|
|
}); |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
var mapAppIcon = function(options) { |
128
|
|
|
if (options.appIcon) { |
129
|
|
|
options.icon = options.appIcon; |
130
|
|
|
delete options.appIcon; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return options; |
134
|
|
|
}; |
135
|
|
|
|
136
|
|
|
var mapText = function(options) { |
137
|
|
|
if (options.text) { |
138
|
|
|
options.message = options.text; |
139
|
|
|
delete options.text; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return options; |
143
|
|
|
}; |
144
|
|
|
|
145
|
|
|
var mapIconShorthand = function(options) { |
146
|
|
|
if (options.i) { |
147
|
|
|
options.icon = options.i; |
148
|
|
|
delete options.i; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return options; |
152
|
|
|
}; |
153
|
|
|
|
154
|
|
|
module.exports.mapToNotifySend = function(options) { |
155
|
|
|
options = mapAppIcon(options); |
156
|
|
|
options = mapText(options); |
157
|
|
|
|
158
|
|
|
for (var key in options) { |
|
|
|
|
159
|
|
|
if (key === 'message' || key === 'title') continue; |
|
|
|
|
160
|
|
|
if (options.hasOwnProperty(key) && notifySendFlags[key] !== key) { |
161
|
|
|
options[notifySendFlags[key]] = options[key]; |
162
|
|
|
delete options[key]; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return options; |
167
|
|
|
}; |
168
|
|
|
|
169
|
|
|
module.exports.mapToGrowl = function(options) { |
170
|
|
|
options = mapAppIcon(options); |
171
|
|
|
options = mapIconShorthand(options); |
172
|
|
|
options = mapText(options); |
173
|
|
|
|
174
|
|
|
if (options.icon && !Buffer.isBuffer(options.icon)) { |
|
|
|
|
175
|
|
|
try { |
176
|
|
|
options.icon = fs.readFileSync(options.icon); |
177
|
|
|
} catch (ex) {} |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return options; |
181
|
|
|
}; |
182
|
|
|
|
183
|
|
|
module.exports.mapToMac = function(options) { |
184
|
|
|
options = mapIconShorthand(options); |
185
|
|
|
options = mapText(options); |
186
|
|
|
|
187
|
|
|
if (options.icon) { |
188
|
|
|
options.appIcon = options.icon; |
189
|
|
|
delete options.icon; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (options.sound === true) { |
193
|
|
|
options.sound = 'Bottle'; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (options.sound === false) { |
197
|
|
|
delete options.sound; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (options.sound && options.sound.indexOf('Notification.') === 0) { |
201
|
|
|
options.sound = 'Bottle'; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if (options.wait === true) { |
205
|
|
|
if (!options.timeout) { |
206
|
|
|
options.timeout = 5; |
207
|
|
|
} |
208
|
|
|
delete options.wait; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
options.json = true; |
212
|
|
|
return options; |
213
|
|
|
}; |
214
|
|
|
|
215
|
|
|
function isArray(arr) { |
216
|
|
|
return Object.prototype.toString.call(arr) === '[object Array]'; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
function noop() {} |
220
|
|
|
module.exports.actionJackerDecorator = function(emitter, options, fn, mapper) { |
221
|
|
|
options = clone(options); |
222
|
|
|
fn = fn || noop; |
223
|
|
|
|
224
|
|
|
if (typeof fn !== 'function') { |
225
|
|
|
throw new TypeError( |
226
|
|
|
'The second argument must be a function callback. You have passed ' + |
227
|
|
|
typeof fn |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return function(err, data) { |
232
|
|
|
var resultantData = data; |
233
|
|
|
var metadata = {}; |
234
|
|
|
// Allow for extra data if resultantData is an object |
235
|
|
|
if (resultantData && typeof resultantData === 'object') { |
236
|
|
|
metadata = resultantData; |
237
|
|
|
resultantData = resultantData.activationType; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
// Sanitize the data |
241
|
|
|
if (resultantData) { |
242
|
|
|
resultantData = resultantData.toLowerCase().trim(); |
243
|
|
|
if (resultantData.match(/^activate|clicked$/)) { |
244
|
|
|
resultantData = 'activate'; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
fn.apply(emitter, [err, resultantData, metadata]); |
249
|
|
|
if (!mapper || !resultantData) return; |
|
|
|
|
250
|
|
|
|
251
|
|
|
var key = mapper(resultantData); |
252
|
|
|
if (!key) return; |
|
|
|
|
253
|
|
|
emitter.emit(key, emitter, options, metadata); |
254
|
|
|
}; |
255
|
|
|
}; |
256
|
|
|
|
257
|
|
|
module.exports.constructArgumentList = function(options, extra) { |
258
|
|
|
var args = []; |
259
|
|
|
extra = extra || {}; |
260
|
|
|
|
261
|
|
|
// Massive ugly setup. Default args |
262
|
|
|
var initial = extra.initial || []; |
263
|
|
|
var keyExtra = extra.keyExtra || ''; |
264
|
|
|
var allowedArguments = extra.allowedArguments || []; |
265
|
|
|
var noEscape = extra.noEscape !== void 0; |
|
|
|
|
266
|
|
|
var checkForAllowed = extra.allowedArguments !== void 0; |
|
|
|
|
267
|
|
|
var explicitTrue = !!extra.explicitTrue; |
268
|
|
|
var keepNewlines = !!extra.keepNewlines; |
269
|
|
|
var wrapper = extra.wrapper === void 0 ? '"' : extra.wrapper; |
|
|
|
|
270
|
|
|
|
271
|
|
|
var escapeFn = function(arg) { |
272
|
|
|
if (isArray(arg)) { |
273
|
|
|
return removeNewLines(arg.join(',')); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
if (!noEscape) { |
277
|
|
|
arg = escapeQuotes(arg); |
278
|
|
|
} |
279
|
|
|
if (typeof arg === 'string' && !keepNewlines) { |
280
|
|
|
arg = removeNewLines(arg); |
281
|
|
|
} |
282
|
|
|
return wrapper + arg + wrapper; |
283
|
|
|
}; |
284
|
|
|
|
285
|
|
|
initial.forEach(function(val) { |
286
|
|
|
args.push(escapeFn(val)); |
287
|
|
|
}); |
288
|
|
|
for (var key in options) { |
289
|
|
|
if ( |
290
|
|
|
options.hasOwnProperty(key) && |
291
|
|
|
(!checkForAllowed || inArray(allowedArguments, key)) |
292
|
|
|
) { |
293
|
|
|
if (explicitTrue && options[key] === true) { |
294
|
|
|
args.push('-' + keyExtra + key); |
295
|
|
|
} else if (explicitTrue && options[key] === false) continue; |
|
|
|
|
296
|
|
|
else args.push('-' + keyExtra + key, escapeFn(options[key])); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
return args; |
300
|
|
|
}; |
301
|
|
|
|
302
|
|
|
function removeNewLines(str) { |
303
|
|
|
var excapedNewline = process.platform === 'win32' ? '\\r\\n' : '\\n'; |
304
|
|
|
return str.replace(/\r?\n/g, excapedNewline); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/* |
308
|
|
|
---- Options ---- |
309
|
|
|
[-t] <title string> | Displayed on the first line of the toast. |
310
|
|
|
[-m] <message string> | Displayed on the remaining lines, wrapped. |
311
|
|
|
[-p] <image URI> | Display toast with an image, local files only. |
312
|
|
|
[-w] | Wait for toast to expire or activate. |
313
|
|
|
[-id] <id> | sets the id for a notification to be able to close it later. |
314
|
|
|
[-s] <sound URI> | Sets the sound of the notifications, for possible values see http://msdn.microsoft.com/en-us/library/windows/apps/hh761492.aspx. |
315
|
|
|
[-silent] | Don't play a sound file when showing the notifications. |
316
|
|
|
[-appID] <App.ID> | Don't create a shortcut but use the provided app id. |
317
|
|
|
-close <id> | Closes a currently displayed notification, in order to be able to close a notification the parameter -w must be used to create the notification. |
318
|
|
|
*/ |
319
|
|
|
var allowedToasterFlags = [ |
320
|
|
|
't', |
321
|
|
|
'm', |
322
|
|
|
'p', |
323
|
|
|
'w', |
324
|
|
|
'id', |
325
|
|
|
's', |
326
|
|
|
'silent', |
327
|
|
|
'appID', |
328
|
|
|
'close', |
329
|
|
|
'install' |
330
|
|
|
]; |
331
|
|
|
var toasterSoundPrefix = 'Notification.'; |
332
|
|
|
var toasterDefaultSound = 'Notification.Default'; |
333
|
|
|
module.exports.mapToWin8 = function(options) { |
334
|
|
|
options = mapAppIcon(options); |
335
|
|
|
options = mapText(options); |
336
|
|
|
|
337
|
|
|
if (options.icon) { |
338
|
|
|
if (/^file:\/+/.test(options.icon)) { |
339
|
|
|
// should parse file protocol URL to path |
340
|
|
|
options.p = url |
341
|
|
|
.parse(options.icon) |
342
|
|
|
.pathname.replace(/^\/(\w:\/)/, '$1') |
343
|
|
|
.replace(/\//g, '\\'); |
344
|
|
|
} else { |
345
|
|
|
options.p = options.icon; |
346
|
|
|
} |
347
|
|
|
delete options.icon; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
if (options.message) { |
351
|
|
|
// Remove escape char to debug "HRESULT : 0xC00CE508" exception |
352
|
|
|
options.m = options.message.replace(/\x1b/g, ''); |
353
|
|
|
delete options.message; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if (options.title) { |
357
|
|
|
options.t = options.title; |
358
|
|
|
delete options.title; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
if (options.appName) { |
362
|
|
|
options.appID = options.appName; |
363
|
|
|
delete options.appName; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
if (typeof options.remove !== 'undefined') { |
367
|
|
|
options.close = options.remove; |
368
|
|
|
delete options.remove; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
if (options.quiet || options.silent) { |
372
|
|
|
options.silent = options.quiet || options.silent; |
373
|
|
|
delete options.quiet; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
if (typeof options.sound !== 'undefined') { |
377
|
|
|
options.s = options.sound; |
378
|
|
|
delete options.sound; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
if (options.s === false) { |
382
|
|
|
options.silent = true; |
383
|
|
|
delete options.s; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
// Silent takes precedence. Remove sound. |
387
|
|
|
if (options.s && options.silent) { |
388
|
|
|
delete options.s; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
if (options.s === true) { |
392
|
|
|
options.s = toasterDefaultSound; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
if (options.s && options.s.indexOf(toasterSoundPrefix) !== 0) { |
396
|
|
|
options.s = toasterDefaultSound; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
if (options.wait) { |
400
|
|
|
options.w = options.wait; |
401
|
|
|
delete options.wait; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
for (var key in options) { |
405
|
|
|
// Check if is allowed. If not, delete! |
406
|
|
|
if ( |
407
|
|
|
options.hasOwnProperty(key) && |
408
|
|
|
allowedToasterFlags.indexOf(key) === -1 |
409
|
|
|
) { |
410
|
|
|
delete options[key]; |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
return options; |
415
|
|
|
}; |
416
|
|
|
|
417
|
|
|
module.exports.mapToNotifu = function(options) { |
418
|
|
|
options = mapAppIcon(options); |
419
|
|
|
options = mapText(options); |
420
|
|
|
|
421
|
|
|
if (options.icon) { |
422
|
|
|
options.i = options.icon; |
423
|
|
|
delete options.icon; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
if (options.message) { |
427
|
|
|
options.m = options.message; |
428
|
|
|
delete options.message; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
if (options.title) { |
432
|
|
|
options.p = options.title; |
433
|
|
|
delete options.title; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
if (options.time) { |
437
|
|
|
options.d = options.time; |
438
|
|
|
delete options.time; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
if (options.q !== false) { |
442
|
|
|
options.q = true; |
443
|
|
|
} else { |
444
|
|
|
delete options.q; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
if (options.quiet === false) { |
448
|
|
|
delete options.q; |
449
|
|
|
delete options.quiet; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
if (options.sound) { |
453
|
|
|
delete options.q; |
454
|
|
|
delete options.sound; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
if (options.t) { |
458
|
|
|
options.d = options.t; |
459
|
|
|
delete options.t; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
if (options.type) { |
463
|
|
|
options.t = sanitizeNotifuTypeArgument(options.type); |
464
|
|
|
delete options.type; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
return options; |
468
|
|
|
}; |
469
|
|
|
|
470
|
|
|
module.exports.isMac = function() { |
471
|
|
|
return os.type() === 'Darwin'; |
472
|
|
|
}; |
473
|
|
|
|
474
|
|
|
module.exports.isMountainLion = function() { |
475
|
|
|
return ( |
476
|
|
|
os.type() === 'Darwin' && |
477
|
|
|
semver.satisfies(garanteeSemverFormat(os.release()), '>=12.0.0') |
478
|
|
|
); |
479
|
|
|
}; |
480
|
|
|
|
481
|
|
|
module.exports.isWin8 = function() { |
482
|
|
|
return ( |
483
|
|
|
os.type() === 'Windows_NT' && |
484
|
|
|
semver.satisfies(garanteeSemverFormat(os.release()), '>=6.2.9200') |
485
|
|
|
); |
486
|
|
|
}; |
487
|
|
|
|
488
|
|
|
module.exports.isLessThanWin8 = function() { |
489
|
|
|
return ( |
490
|
|
|
os.type() === 'Windows_NT' && |
491
|
|
|
semver.satisfies(garanteeSemverFormat(os.release()), '<6.2.9200') |
492
|
|
|
); |
493
|
|
|
}; |
494
|
|
|
|
495
|
|
|
function garanteeSemverFormat(version) { |
496
|
|
|
if (version.split('.').length === 2) { |
497
|
|
|
version += '.0'; |
498
|
|
|
} |
499
|
|
|
return version; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
function sanitizeNotifuTypeArgument(type) { |
503
|
|
|
if (typeof type === 'string' || type instanceof String) { |
504
|
|
|
if (type.toLowerCase() === 'info') return 'info'; |
|
|
|
|
505
|
|
|
if (type.toLowerCase() === 'warn') return 'warn'; |
|
|
|
|
506
|
|
|
if (type.toLowerCase() === 'error') return 'error'; |
|
|
|
|
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
return 'info'; |
510
|
|
|
} |
511
|
|
|
|